﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Model;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class GonggaoDAL
    {
        DBHelper db = new DBHelper();

        public List<Gonggao> GetListAll()
        {
            string sql = $"select * from [dbo].[Gonggao]";
            List<Gonggao> list = new List<Gonggao>();
            DataTable dt = db.GetData(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Gonggao DM = new Gonggao();
                DM.GonggaoID = Convert.ToInt32(dt.Rows[i]["GonggaoID"].ToString());
                DM.GonggaoName = dt.Rows[i]["GonggaoName"].ToString();
                DM.GonggaoTime = Convert.ToDateTime(dt.Rows[i]["GonggaoTime"].ToString());
                DM.NowState = Convert.ToInt32(dt.Rows[i]["NowState"].ToString());
                list.Add(DM);
            }
            return list;
        }
        public List<Gonggao> GetListAllt()
        {
            string sql = $"select top 3 * from [dbo].[Gonggao]";
            List<Gonggao> list = new List<Gonggao>();
            DataTable dt = db.GetData(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Gonggao DM = new Gonggao();
                DM.GonggaoID = Convert.ToInt32(dt.Rows[i]["GonggaoID"].ToString());
                DM.GonggaoName = dt.Rows[i]["GonggaoName"].ToString();
                DM.GonggaoTime = Convert.ToDateTime(dt.Rows[i]["GonggaoTime"].ToString());
                DM.NowState = Convert.ToInt32(dt.Rows[i]["NowState"].ToString());
                list.Add(DM);
            }
            return list;
        }
        public bool Update(Gonggao DM)
        {
            string sql = $"update [dbo].[Gonggao] set NowState='{DM.NowState}' where GonggaoID='{DM.GonggaoID}'";
            if (db.SetData(sql))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool ADD(Gonggao DM)
        {
            string sql = $"insert into [dbo].[Gonggao] values('{DM.GonggaoName}','{DM.GonggaoTime}','')";
            if (db.SetData(sql))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool ADDS(Gonggao DM)
        {
            string sql = $"insert into[dbo].[Achilve] values('{DM.StudentNames}','{DM.StudentIDs}','{DM.StudentSubject}','{DM.StudentAchilve}','')";
            if (db.SetData(sql))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
